package it.uniroma2.art.coda.pearl.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProjectionRulesModel {
	private static Logger logger = LoggerFactory.getLogger(ProjectionRulesModel.class);

	private Map<String, Collection<ProjectionRule>> typeNameToNonLazyRulesMap;
	private Map<String, ProjectionRule> idToRuleMap;

	public ProjectionRulesModel() {
		initialize();
	}

	public void initialize() {
		typeNameToNonLazyRulesMap = new HashMap<String, Collection<ProjectionRule>>();
		idToRuleMap = new HashMap<String, ProjectionRule>();
	}

	public void addProjectionRule(ProjectionRule pr) {
		if (pr.isLazy() == false) {
			if (typeNameToNonLazyRulesMap.containsKey(pr.getUIMAType()) == false) {
				typeNameToNonLazyRulesMap.put(pr.getUIMAType(), new ArrayList<ProjectionRule>());
			}
			Collection<ProjectionRule> projectionRuleList = typeNameToNonLazyRulesMap.get(pr.getUIMAType());
			projectionRuleList.add(pr);
		}
		if (pr.isIdNull() == false)
			idToRuleMap.put(pr.getId(), pr);
	}

	public Collection<ProjectionRule> getNonLazyProjectionRulesByTypeName(String typeName) {
		Collection<ProjectionRule> result = typeNameToNonLazyRulesMap.get(typeName);
		if(result != null)
			return result;
		else
			return new ArrayList<ProjectionRule>();
	}

	public Map<String, Collection<ProjectionRule>> getMapProjectionRules() {
		return typeNameToNonLazyRulesMap;
	}

	public ProjectionRule getProjRuleFromId(String id) {
		return idToRuleMap.get(id);
	}
}
